package tu.java.reservation;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import tu.java.db.DBConnManager;

public class Ticket {
	
	
	
	private int	ticketId;
	private int	seatId;
	
	
	
	public void addTicket(Connection conn, PreparedStatement pstmt, int reservationId) throws SQLException {
		pstmt = conn.prepareStatement("INSERT INTO TT02_TICKETS(CH02_SEAT_ID,CR01_RESERVATION_ID) VALUES(?,?)");
		pstmt.setInt(1, seatId);
		pstmt.setInt(2, reservationId);
		pstmt.executeUpdate();
	}
	
	
	
	public static Vector<Ticket> searchTickets(int reservationId) {
		Vector<Ticket> tickets = new Vector<Ticket>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			pstmt = conn.prepareStatement("SELECT CT02_TICKET_ID,CH02_SEAT_ID FROM TT02_TICKETS WHERE CR01_RESERVATION_ID=?");
			pstmt.setInt(1, reservationId);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				Ticket t = new Ticket();
				t.ticketId = rs.getInt(1);
				t.seatId = rs.getInt(2);
				tickets.add(t);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return tickets;
	}
	
	
	
	public boolean annulTicket() {
		boolean status = true;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			pstmt = conn.prepareStatement(" DELETE FROM TT02_TICKETS WHERE CT02_TICKET_ID=?");
			pstmt.setInt(1, ticketId);
			pstmt.executeUpdate();
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return status;
	}
	
	
	
	public int getTicketId() {
		return ticketId;
	}
	
	
	
	public void setTicketId(int ticketId) {
		this.ticketId = ticketId;
	}
	
	
	
	public int getSeatId() {
		return seatId;
	}
	
	
	
	public void setSeatId(int seatId) {
		this.seatId = seatId;
	}
	
}
